﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KL.Store.Core.Domain;
using TN.Data;

namespace KL.Store.Data
{
    public class dbReportPanel
    {
        #region Son Satışlar Dashboard
        public static IList<Sales> GetDashboardSales(int ItemCount)
        {
            IList<Sales> objectList = new List<Sales>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_Report_Last_Sales", objectList, new object[] { ItemCount });

            return objectList;
        }
        #endregion

        #region Stok Toplam Listesi
        public static IList<TotalStock> StockTotalList()
        {
            IList<TotalStock> objectList = new List<TotalStock>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_Report_Total_Sales_Report", objectList);

            return objectList;
        }
        #endregion

        #region Kritik Stok
        public static IList<CriticalStocks> GetCriticalStocks(int StockCount, int MaximumStock, int AmbarId)
        {
            IList<CriticalStocks> objectList = new List<CriticalStocks>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_Report_Critical_Stocks", objectList, new object[] { StockCount, MaximumStock, AmbarId });

            return objectList;
        }
        #endregion

        #region Ambar Listesi
        public static IList<AmbarListesi> AmbarListesi()
        {
            IList<AmbarListesi> objectList = new List<AmbarListesi>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_Report_Ambar_List", objectList);
            return objectList;
        }
        #endregion

        #region Filtrelenmiş Satış Listesi
        public static IList<Sales> GetFilteredSales(int IdAmbar, DateTime BasTar, DateTime BitTar, string DovizId)
        {
            IList<Sales> objectList = new List<Sales>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_Report_Products_Sales", objectList, new object[] { IdAmbar, BasTar, BitTar, DovizId });

            return objectList;
        }
        #endregion

        #region Kredi Kartı İşlem Listesi
        public static IList<CCProcess> CCProcessList()
        {
            IList<CCProcess> objectList = new List<CCProcess>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_CCProcessOrder_List", objectList);

            return objectList;
        }
        #endregion

        #region Sipariş Listesi(Satış)

        public static IList<SiparisSatis> SiparisListesiSatis(bool withCancelAndComplete, DateTime startTime, DateTime endTime)
        {
            IList<SiparisSatis> objectList = new List<SiparisSatis>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_Report_Sales_List", objectList, new object[] { withCancelAndComplete, startTime, endTime });

            return objectList;
        }

        #endregion

        #region Satış Durumları
        public static IList<SiparisDurum> SiparisDurumlari()
        {
            IList<SiparisDurum> objectList = new List<SiparisDurum>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_Report_Sales_Durum", objectList);

            return objectList;
        }
        #endregion

        #region Sipariş Durum Güncelle
        public static int SiparisDurumGuncelle(int SiparisId, int Tip, int DurumId)
        {
            int id = (int)SqlHelper.ExecuteScalar(dbParameters.dsnStore, "SPR_ESTORE_Report_Sales_Durum_Update",
                                                  new object[] { SiparisId, Tip, DurumId });

            return id;
        }
        #endregion

        #region Sipariş Durum Güncelle Teslim
        public static int SiparisDurumGuncelleTeslim(int siparisId, int Tip, int durumId, DateTime date)
        {
            int id = (int)SqlHelper.ExecuteScalar(dbParameters.dsnStore, "SPR_ESTORE_Report_Sales_Durum_Update_Date",
                                                  new object[] { siparisId, Tip, durumId, date });

            return id;
        }
        #endregion

        #region Sipariş Sonraki Durum Ayarla

        public static int SiparisDurumSonraki(int siparisId, int tip)
        {
            var id = (int)SqlHelper.ExecuteScalar(dbParameters.dsnStore, "SPR_ESTORE_Report_Sales_Set_NextStatus", new object[] { siparisId, tip });

            return id;
        }
        #endregion

        #region Sipariş Detayı
        
        public static IList<SiparisSatisDetay> SiparisDetay(int SiparisId, int Tip)
        {
            IList<SiparisSatisDetay> objectList = new List<SiparisSatisDetay>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_Report_Sales_GetDetail", objectList, new object[] { SiparisId, Tip });

            return objectList;
        }

        #endregion

        #region Sipariş Adresleri

        public static IList<OrderAddress> SiparisAdresleri(int SiparisId, int Tip)
        {
            IList<OrderAddress> objectList = new List<OrderAddress>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_Report_Sales_GetDetail_Addresses", objectList, new object[] { SiparisId, Tip });

            return objectList;
        }

        #endregion

        #region Sipariş Bilgi

        public static SiparisSatisBilgi SiparisBilgi(int SiparisId, int Tip)
        {
            IList<SiparisSatisBilgi> objectList = new List<SiparisSatisBilgi>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_Report_Sales_Get", objectList, new object[] { SiparisId, Tip });

            return objectList.FirstOrDefault();
        }

        public static SiparisSatis SiparisBilgiFatura(int SiparisId, int Tip)
        {
            IList<SiparisSatis> objectList = new List<SiparisSatis>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_Report_Sales_Get_Voucher", objectList, new object[] { SiparisId, Tip });

            return objectList.FirstOrDefault();
        }

        #endregion

        #region CategoryMenuListGet

        public static IList<CategoryMenuList> CategoryMenuListGet()
        {
            IList<CategoryMenuList> objectList = new List<CategoryMenuList>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_Category_List_All", objectList);

            return objectList;
        }

        public static string CategoryMenuListHtml(IList<CategoryMenuList> objectList, int selectedCategory)
        {
            int ParentId = objectList.Min(z => z.ParentCategoryId);
            var firstCat = objectList.Where(f => f.CategoryId == ParentId).FirstOrDefault();
            objectList = objectList.Where(o => o.CategoryId != ParentId).ToList();
            StringBuilder sb = new StringBuilder();
            int NestedLevel = 0;

            IList<int> selectedParents = new List<int>();
            selectedParents.Add(0);
            if (selectedCategory > 0 && objectList.Where(o => o.CategoryId == selectedCategory).Count() > 0)
            {
                var processedCat = selectedCategory;
                while (true)
                {
                    if (processedCat == 1)
                    {
                        break;
                    }
                    selectedParents.Add(objectList.Where(o => o.CategoryId == processedCat).FirstOrDefault().ParentCategoryId);
                    processedCat = objectList.Where(o => o.CategoryId == processedCat).FirstOrDefault().ParentCategoryId;
                }
            }

            sb.Append("<ul data-level=\"0\" class=\"nestedlevel0\">");
            sb.Append("<li class=\"list-nested-level-" + NestedLevel.ToString() + "\" data-level=\"0\" id=\"category" + firstCat.CategoryId.ToString() + "\">");
            sb.Append("<i class=\"glyphicon glyphicon-minus\"></i>");
            sb.Append("<a href=\"javascript:void(0);\" id=\"caturl" + firstCat.CategoryId.ToString() + "\" onclick=\"selectCategoryForEdit(" + firstCat.CategoryId.ToString() + ");\">" + firstCat.CategoryName + " (" + firstCat.CategoryId.ToString() + ")</a>");
            sb.Append("<ul data-level=\"1\">");
            NestedLevel++;
            foreach (CategoryMenuList cm in objectList.Where(o => o.ParentCategoryId == ParentId))
            {
                var actv = selectedCategory == cm.CategoryId ? " active" : "";
                sb.Append("<li class=\"list-nested-level-" + NestedLevel.ToString() + "\" data-level=\"" + NestedLevel.ToString() + "\" id=\"category" + cm.CategoryId.ToString() + "\">");
                if (objectList.Where(o => o.ParentCategoryId == cm.CategoryId).Count() > 0)
                {
                    if (selectedParents.Contains(cm.CategoryId))
                    {
                        sb.Append("<i class=\"glyphicon glyphicon-minus\"></i>");
                    }
                    else
                    {
                        sb.Append("<i class=\"glyphicon glyphicon-plus\"></i>");
                    }
                }
                sb.Append("<a href=\"javascript:void(0);\" class=\"" + actv + "\" id=\"caturl" + cm.CategoryId.ToString() + "\" onclick=\"selectCategoryForEdit(" + cm.CategoryId.ToString() + ");\">" + cm.CategoryName + " (" + cm.CategoryId.ToString() + ")</a>");
                if (objectList.Count() > 0)
                {
                    sb.Append(CategoryMenuListHtmlSubs(objectList.Where(o => o.CategoryId != cm.CategoryId && o.CategoryId != o.ParentCategoryId).ToList(), cm.CategoryId, NestedLevel, selectedCategory, selectedParents));
                }
                sb.Append("</li>");
            }
            sb.Append("</ul>");
            sb.Append("</li>");
            sb.Append("</ul>");
            return sb.ToString();
        }

        public static string CategoryMenuListHtmlSubs(IList<CategoryMenuList> objectList, int ParentId, int NestedLevel, int selectedCategory, IList<int> selectedParents)
        {
            NestedLevel++;
            StringBuilder sb = new StringBuilder();
            sb.Append("<ul data-level=\"" + NestedLevel.ToString() + "\"");
            if (NestedLevel >= 2 && !selectedParents.Contains(ParentId))
            {
                sb.Append(" style=\"display:none;\"");
            }
            sb.Append(">");
            foreach (CategoryMenuList cm in objectList.Where(o => o.ParentCategoryId == ParentId))
            {
                var slct = selectedCategory == cm.CategoryId ? " selectedCat" : "";
                var actv = selectedCategory == cm.CategoryId ? " active" : "";
                sb.Append("<li class=\"list-nested-level-" + NestedLevel.ToString() + slct + "\" data-level=\"" + NestedLevel.ToString() + "\" id=\"category" + cm.CategoryId.ToString() + "\">");
                if (objectList.Where(o => o.ParentCategoryId == cm.CategoryId).Count() > 0)
                {
                    if (selectedParents.Contains(cm.CategoryId))
                    {
                        sb.Append("<i class=\"glyphicon glyphicon-minus\"></i>");
                    }
                    else
                    {
                        sb.Append("<i class=\"glyphicon glyphicon-plus\"></i>");
                    }
                }
                sb.Append("<a href=\"javascript:void(0);\" class=\"" + actv + "\" id=\"caturl" + cm.CategoryId.ToString() + "\" onclick=\"selectCategoryForEdit(" + cm.CategoryId.ToString() + ");\">" + cm.CategoryName + " (" + cm.CategoryId.ToString() + ")</a>");
                if (objectList.Where(o => o.ParentCategoryId == ParentId).Count() > 0)
                {
                    sb.Append(CategoryMenuListHtmlSubs(objectList.Where(o => o.CategoryId != cm.CategoryId).ToList(), cm.CategoryId, NestedLevel, selectedCategory, selectedParents));
                }
                sb.Append("</li>");
            }
            sb.Append("</ul>");
            return sb.ToString();
        }

        #endregion

        #region Kategori Düzenleme Get

        public static CategoryEdit GetCategoryEdit(int CategoryId)
        {
            IList<CategoryEdit> objectList = new List<CategoryEdit>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_Category_Edit_Get", objectList, new object[] { CategoryId });
            return objectList.FirstOrDefault();
        }

        public static IList<CategoryExtraFields> GetCategoryExtraFields(int CategoryId)
        {
            IList<CategoryExtraFields> objectList = new List<CategoryExtraFields>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_Category_Edit_Get_Extra", objectList, new object[] { CategoryId });
            return objectList;
        }

        #endregion

        #region Kategori Düzenleme Save

        public static int SaveCategoryEdit(CategoryEdit Category)
        {
            int result = (int)SqlHelper.ExecuteScalar(
                dbParameters.dsnStore,
                "SPR_ESTORE_Category_Edit_Save",
                new object[] {
                    Category.OrgCategoryId,
                    Category.CategoryId,
                    Category.ParentCategoryId,
                    Category.CategoryName,
                    Category.CategoryType,
                    Category.ProductGroupCode,
                    Category.GroupCode,
                    Category.WebDescription,
                    Category.WebKeywords,
                    Category.ShowOnEStore,
                    Category.BannerActive,
                    Category.eStoreRowCount,
                    Category.eStoreImageType,
                    Category.BasketGroupNo,
                    Category.BasketMinPrice,
                    Category.BasketMinNotice,
                    Category.ProductQuestionString,
                    Category.CargoPrice,
                    Category.SuggestedGroup,
                    Category.ShowcaseGroup,
                    Category.MaxInstallmentCount,
                    Category.ProductListCount,
                    Category.InstallmentPrice,
                    Category.Desi,
                    Category.FreeCargoInCity,
                    Category.FreeCargo,
                    Category.Transport,
                    Category.WebTitle,
                    Category.Canonical,
                    Category.ShowModelOnProductDetails
                });
            return result;
        }

        #endregion

        #region Kategori Ekle

        public static int AddNewCategory(CategoryEdit Category)
        {
            int result = (int)SqlHelper.ExecuteScalar(
     dbParameters.dsnStore,
     "SPR_ESTORE_Category_Add_New",
     new object[] {
                    Category.ParentCategoryId,
                    Category.CategoryName,
                    Category.CategoryType,
                    Category.WebDescription,
                    Category.WebKeywords,
                    Category.ShowOnEStore,
                    Category.BannerActive,
                    Category.eStoreRowCount,
                    Category.eStoreImageType,
                    Category.BasketGroupNo,
                    Category.BasketMinPrice,
                    Category.BasketMinNotice,
                    Category.ProductQuestionString,
                    Category.CargoPrice,
                    Category.SuggestedGroup,
                    Category.ShowcaseGroup,
                    Category.MaxInstallmentCount,
                    Category.ProductListCount,
                    Category.InstallmentPrice,
                    Category.Desi,
                    Category.FreeCargoInCity,
                    Category.FreeCargo,
                    Category.Transport,
                    Category.WebTitle,
                    Category.ShowModelOnProductDetails
                });
            return result;
        }

        #endregion

        #region Kategori Ekstra Alan

        public static int UpdateExtraField(string Field, string Value, int CategoryId, int SatirNo, string Original)
        {

            int result = (int)SqlHelper.ExecuteScalar(dbParameters.dsnStore, "SPR_ESTORE_Category_EkstraAlan_Guncelle", new object[] { Field, Value, CategoryId, SatirNo, Original });
            return result;
        }

        public static int AddExtraField(int CategoryId, int SatirNo, string Aciklama, string Bilgi, short EstoreGoster)
        {

            int result = (int)SqlHelper.ExecuteScalar(dbParameters.dsnStore, "SPR_ESTORE_Category_EkstraAlan_Ekle", new object[] { CategoryId, SatirNo, Aciklama, Bilgi, EstoreGoster });
            return result;
        }

        #endregion

        #region Kategori Sil

        public static int RemoveCategory(int CategoryId)
        {
            int result = (int)SqlHelper.ExecuteScalar(dbParameters.dsnStore, "SPR_ESTORE_Category_Remove", new object[] { CategoryId });
            return result;
        }

        #endregion

        #region Ürün Alan Güncelle (Filtre)

        public static int UpdateProductCheckStock(int ProductId, string Value)
        {
            int result = (int)SqlHelper.ExecuteScalar(dbParameters.dsnStore, "SPR_ESTORE_Product_Update_CheckStock", new object[] { ProductId, Value });
            return result;
        }

        public static int UpdateProductBitField(int ProductId, string Field, string Value)
        {
            int result = (int)SqlHelper.ExecuteScalar(dbParameters.dsnStore, "SPR_ESTORE_Product_Update_BitField", new object[] { ProductId, Field, Value });
            return result;
        }

        public static int UpdateProductTextField(int ProductId, string Field, string Value)
        {
            int result = (int)SqlHelper.ExecuteScalar(dbParameters.dsnStore, "SPR_ESTORE_Product_Update_TextField", new object[] { ProductId, Field, Value });
            return result;
        }

        #endregion

    }
}
